package com.yjd.pub.merchant.service;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.enums.ReferenceTypeEnum;
import com.yjd.comm.fs.util.FileUtil;
import com.yjd.comm.project.model.ProjectModel;
import com.yjd.comm.project.service.IProjectService;
import com.yjd.comm.resource.model.ResourceModel;
import com.yjd.comm.resource.service.IResourceService;
import com.yjd.comm.util.FrameUtil;
import com.yjd.comm.util.ServiceHolder;
import com.yjd.comm.util.StringUtil;
import org.springframework.stereotype.Service;

import com.yjd.comm.base.dao.IBaseDao;
import com.yjd.comm.base.service.impl.BaseServiceImpl;
import com.yjd.comm.merchant.model.MerchantProjectVerifyModel;
import com.yjd.comm.merchant.service.IMerchantProjectVerifyService;
import com.yjd.pub.merchant.dao.IMerchantProjectVerifyDao;

/**
 * <b>description</b>：商户商品审核表业务实现<br>
 * <b>time</b>：2018-03-29 16:04:03 <br>
 * <b>author</b>：  carl
 */
@Service("merchantProjectVerifyService")
public class MerchantProjectVerifyServiceImpl extends BaseServiceImpl implements IMerchantProjectVerifyService {

	/**
	 * 插入
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public MerchantProjectVerifyModel insert(MerchantProjectVerifyModel model) throws Exception {
		return this.merchantProjectVerifyDao.insertModel(model, true);
	}

	/**
	 * 更新
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public int update(MerchantProjectVerifyModel model) throws Exception {
		return this.merchantProjectVerifyDao.updateModel(model, true);
	}

	/**
	 * 根据id删除
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public int delete(long id) throws Exception {
		Map<Object, Object> paramMap = new HashMap<Object, Object>();
		paramMap.put("id", id);
		return this.merchantProjectVerifyDao.deleteModel(paramMap, true);
	}

	@Override
	public int updateByMap(Map param) throws Exception {
		return this.merchantProjectVerifyDao.updateByMap(param,true);
	}

	@Resource
	private IMerchantProjectVerifyDao merchantProjectVerifyDao;

	@Resource
	private IProjectService projectService;

	@Resource
	private IResourceService resourceService;

	@Override
	public IBaseDao getBaseDao() {
		return this.merchantProjectVerifyDao;
	}

	@Override
	public void updateAfterCheckout(Map param)throws Exception {

		int rows = this.merchantProjectVerifyDao.updateByMap(param,true);
		if(rows != 1){
            FrameUtil.throwBaseException1("商品审核失败,请重试！");
        }
		MerchantProjectVerifyModel model = merchantProjectVerifyDao.getModelById(param.get("id"), false);
		//判断审核状态 1 通过，2 拒绝
        Integer status = Integer.valueOf(param.get("status").toString());
        if (status == 2){
            return;
        }
        if (status == 1) {
            ProjectModel projectModel = new ProjectModel();
            projectModel.setProject_name(model.getProject_name());
            projectModel.setProject_price(model.getProject_price());
            //初始化商品时，默认商品售价=项目价格
			projectModel.setAct_price(model.getProject_price());
            projectModel.setIntro(model.getIntro());
            projectModel.setOrg_id(model.getOrg_id());
            projectModel.setCategory_id(model.getCategory_id().longValue());
            projectModel.setTime_limit(model.getTime_limit());
            projectModel.setTimes(model.getTimes());
            projectModel.setCreate_time(FrameUtil.getTime());
            projectModel.setStatus(0);
            projectModel.setIs_used(0);

            ProjectModel pm = projectService.insert(projectModel);
            if (pm == null || pm.getProject_id() == null) {
                FrameUtil.throwBaseException1("审核通过，添加商品失败！");
            }

			//拷贝商品图片至新目录，并删除原图片
			if (StringUtil.isNotEmpty(model.getProject_pic())){
				String pic_path = FileUtil.copyFileOnRemote(model.getProject_pic(), ReferenceTypeEnum.PROJECT, pm.getProject_id());
				if (StringUtil.isEmpty(pic_path)) {
					FrameUtil.throwBaseException1("图片拷贝失败！");
				}
				projectService.updateByMap(FrameUtil.newHashMap("project_id",pm.getProject_id(), "project_pic",pic_path));
				merchantProjectVerifyDao.updateByMap(FrameUtil.newHashMap("id",model.getId(), "project_pic", pic_path), true);
				// 记录本地信息
				Long currentTime = FrameUtil.getTime();
				ResourceModel res = new ResourceModel();
				res.setRef_type(ReferenceTypeEnum.PROJECT.getCode());
				res.setRef_id(pm.getProject_id());
				res.setCreate_time(currentTime);
				res.setLast_update_time(currentTime);
				res.setPath(pic_path);
				if (resourceService.insert(res)==null){
					FrameUtil.throwBaseException1("插入本地图片信息失败！");
				}

				if (!StringUtil.isEmpty(pic_path)) {
					FileUtil.delteOssFile(pm.getProject_pic());
				}
			}
		}
	}

}
